package
{
	import flash.events.Event;
	import flash.net.SharedObject;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.utils.Dictionary;
	
	import org.flixel.FlxG;
	import org.flixel.FlxGroup;
	import org.flixel.FlxSprite;
	import org.flixel.FlxText;
	
	public class ModalSavePopup extends FlxGroup
	{
		protected var Background:FlxSprite;
		
		protected var nameSpace:TextField;
		protected var name:String;
		
		protected var saveButton:FlxSprite;
		protected var cancelButton:FlxSprite;
		
		protected var saveBG:FlxSprite;
		
		protected var localInformation:SharedObject;
		
		protected var curMapNames:Array;
		
		//protected var alreadyExists:Boolean;
		//protected var existsText:FlxText;
		
		protected var spec:PuzzleSpecification;
		
		protected var done:Boolean;
		protected var saved:Boolean;
		
		
		public function ModalSavePopup(mapSpec:PuzzleSpecification)
		{
			spec = mapSpec;
			done = false;
			saved = false;
			localInformation = SharedObject.getLocal("elementalGameInfo");
			
			var customMaps:String = localInformation.data["customMaps"];
			
			if (customMaps != null) {
				curMapNames = customMaps.split(",");
			} else {
				curMapNames = new Array();
			}
			
			Background = new FlxSprite(83,113,Res.popupBackground);
			
			this.add(Background);
			
			var titleText:FlxText = new FlxText(107, 137, 300, "Name Your Puzzle", false);
			titleText.setFormat("Arial", 30, 0x111111, "center");
			this.add(titleText);
			
//			existsText = new FlxText(158, 325, 200, "Name Already Exists", false);
//			existsText.setFormat("Arial", 20, 0x000000, "center");
			
			nameSpace = new TextField();
			nameSpace.type = TextFieldType.INPUT;
			nameSpace.x = 117;
			nameSpace.y = 242;
			nameSpace.width = 280;
			nameSpace.height = 30;
			nameSpace.maxChars = 20;
			nameSpace.multiline = false;
			nameSpace.restrict = "a-zA-Z0-9";
			nameSpace.text = new String("untitled");
			nameSpace.setTextFormat(new TextFormat("Arial", 24, 0x000000));
			nameSpace.addEventListener(Event.CHANGE, updateName);
			
			saveButton = new FlxSprite(158, 400, Res.saveButton);
			cancelButton = new FlxSprite(283, 400, Res.cancelButton);
			saveBG = new FlxSprite(107,237,Res.saveBG);
			
			this.add(saveButton);
			this.add(cancelButton);
			this.add(saveBG);
			
			name = "untitled";
			
		}
		
		public override function update():void  {
			super.update();
			
			if (FlxG.mouse.justPressed()) {
				checkSave();
				checkCancel();
			}
			
		}
		
		private function checkSave():void {
			if (saveButton.overlapsPoint(FlxG.mouse.x, FlxG.mouse.y)) {
//				if (nameAlreadyExists()) {
//					alreadyExists = true;
//					this.add(existsText);
//				} else {
				if (name != null && name != "") {
					
					saved = true;
					var newName:String = "cm_" + name;
					if (curMapNames.length == 0) {
						localInformation.data["customMaps"] = newName;
					} else {
						localInformation.data["customMaps"] += "," + newName;
					}
					localInformation.data[newName + "_map"] = spec.getMap();
					localInformation.data[newName + "_tst"] = spec.getTileSet();
					localInformation.data[newName + "_arw"] = getArrayString(spec.getArrows());
					localInformation.data[newName + "_chi"] = getArrayString(spec.getChickens());
					localInformation.data[newName + "_mapID"] = spec.getMapID();
					localInformation.flush();
					FlxG.stage.removeChild(nameSpace);
					done = true;
				}
				//}
			}
		}
		
		private function getArrayString(a:Array):String {
			if (a.length == 0) return "";
			var res:String = "" + a[0];
			for (var i:int = 1; i < a.length; i++) {
				res += ","+a[i];
			}
			return res;
		}
		
		private function checkCancel():void {
			if (cancelButton.overlapsPoint(FlxG.mouse.x, FlxG.mouse.y)) {
				done = true;
			}
		}
		
		private function updateName(e:Event):void {
			name = nameSpace.text;
			//if (alreadyExists) this.remove(existsText, true);
		}
		
		public function displayNameText():void {
			FlxG.stage.addChild(nameSpace);
		}
		
		
		public function removeNameText():void {
			FlxG.stage.removeChild(nameSpace);
		}
		
//		private function nameAlreadyExists():Boolean {
//			for (var i:int = 0; i < curMapNames.length; i++) {
//				if (curMapNames[i] == name) return true;
//			}
//			return false;
//		}
		
		public function finishedPopup():Boolean {
			return done;
		}
		
		public function wasSaved():Boolean {
			return saved;
		}
	}
}